package com.eternal.peaceworld.autoconfigure;

import com.eternal.peaceworld.service.AesEncryptorService;
import com.eternal.peaceworld.service.EncryptorService;
import com.eternal.peaceworld.service.Sm4EncryptorService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName EncryptorAutoConfiguration
 * @PackageName encryptorspringbootautoconfigure.com.eternal.peaceworld.autoconfigure
 * @projectName encryptor-spring-boot-parent
 * @Description 加密器自动配置类
 * @Author games
 * @Date 2025/8/17 下午12:44
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties(EncryptorProperties.class)
public class EncryptorAutoConfiguration {

    /**
     * 创建AES加密器服务实例
     * 当配置 encryptor.type=aes 时启用此配置
     */
    @Bean
    @ConditionalOnProperty(name = "encryptor.type", havingValue = "aes")
    @ConditionalOnMissingBean
    public EncryptorService aesEncryptorService(EncryptorProperties properties) {
        return new AesEncryptorService(properties.getKey());
    }

    /**
     * 创建SM4加密器服务实例
     * 当配置 encryptor.type=sm4 时启用此配置
     */
    @Bean
    @ConditionalOnProperty(name = "encryptor.type", havingValue = "sm4")
    @ConditionalOnMissingBean
    public EncryptorService sm4EncryptorService(EncryptorProperties properties) {
        return new Sm4EncryptorService(properties.getKey());
    }
}
